/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * FrmRestriction.java
 *
 * Created on 20-ago-2009, 19:45:42
 */
package gui.forms;

import gui.tools.Receiver;
import java.awt.Image;
import java.util.Collection;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import lib.model.FactoryModel;
import lib.model.ItemCategory;
import lib.model.Restriction;

/**
 *
 * @author jorge
 */
public class FrmRestriction extends javax.swing.JDialog {

    private Restriction restriction;
    private Receiver parent;

    /** Creates new form FrmRestriction */
    public FrmRestriction(Restriction restriction,
            FactoryModel model,
            Receiver parent) {
        initComponents();

        this.getRootPane().setDefaultButton(btnSave);

        String fileString = "/res/icons/logo.png";
        try {
            Image icon = ImageIO.read(getClass().getResource(fileString));
            this.setIconImage(icon);
        } catch (java.io.IOException ioe) {
            System.out.println("Error loading file: " + fileString);
        }

        this.restriction = restriction;
        this.parent = parent;
        if (model != null) {
            Collection<ItemCategory> ics = model.getItemCategories().values();
            for (ItemCategory ic : ics) {
                cbItemCategory.addItem(ic.getName());
            }
        }
        if (restriction != null) {
            cbItemCategory.setSelectedItem(restriction.getItemCategory());
            txtMinBatch.setText(String.valueOf(restriction.getMinBatch()));
            txtMaxBatch.setText(String.valueOf(restriction.getMaxBatch()));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblItemCategory = new javax.swing.JLabel();
        cbItemCategory = new javax.swing.JComboBox();
        lblMinBatch = new javax.swing.JLabel();
        lblMaxBatch = new javax.swing.JLabel();
        txtMinBatch = new javax.swing.JTextField();
        txtMaxBatch = new javax.swing.JTextField();
        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();

        setModal(true);
        setResizable(false);

        lblItemCategory.setText("Item category:");

        lblMinBatch.setText("Minimum batch:");

        lblMaxBatch.setText("Maximum batch:");

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblMaxBatch)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblItemCategory)
                            .addComponent(lblMinBatch))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnSave)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnCancel))
                            .addComponent(cbItemCategory, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtMinBatch, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtMaxBatch, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblItemCategory)
                    .addComponent(cbItemCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMinBatch)
                    .addComponent(txtMinBatch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMaxBatch)
                    .addComponent(txtMaxBatch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnSave))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-283)/2, (screenSize.height-186)/2, 283, 186);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        try {
            restriction = new Restriction(cbItemCategory.getSelectedItem().toString(),
                    Integer.parseInt(txtMinBatch.getText()),
                    Integer.parseInt(txtMaxBatch.getText()));
            this.setVisible(false);
            parent.reciveObject(restriction);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Minimum batch must be at least zero and\n" +
                    "maximum batch must be bigger than zero.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }


    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbItemCategory;
    private javax.swing.JLabel lblItemCategory;
    private javax.swing.JLabel lblMaxBatch;
    private javax.swing.JLabel lblMinBatch;
    private javax.swing.JTextField txtMaxBatch;
    private javax.swing.JTextField txtMinBatch;
    // End of variables declaration//GEN-END:variables
}
